﻿using Abp.AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.WizardAgent
{
    /// <summary>
    /// Wizard提交订单的请求参数
    /// </summary>
    public class SubmitOrderRequest : IRequest
    {
        /// <summary>
        /// 优惠费用（比如：银联直减、银联折上折）
        /// </summary>
        public double DiscountCost { get; set; }

        /// <summary>
        /// AV银行账号
        /// </summary>
        public string AV { get; set; }

        /// <summary>
        /// 货币单位
        /// </summary>
        public string CurrencyCode { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string ReservationNumber { get; set; }

        #region 取还车信息
        /// <summary>
        /// 取车时间
        /// </summary>
        public DateTime PickupDateTime { get; set; }

        /// <summary>
        /// 取车门店编号
        /// </summary>
        public string PickUpStoreCode { get; set; }

        /// <summary>
        /// 还车时间
        /// </summary>
        public DateTime ReturnDateTime { get; set; }

        /// <summary>
        /// 还车门店编号
        /// </summary>
        public string ReturnStoreCode { get; set; }
        #endregion

        /// <summary>
        /// 客户的姓
        /// </summary>
        public string Surname { get; set; }

        /// <summary>
        /// 客户的名
        /// </summary>
        public string Firstname { get; set; }

        /// <summary>
        /// 客户地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 客户的邮政编码
        /// </summary>
        public string Postcode { get; set; }

        /// <summary>
        /// 客户所在城市
        /// </summary>
        public string CustomerCity { get; set; }

        /// <summary>
        /// 客户所在的国家名称
        /// </summary>
        public string CustomerCountryName { get; set; }

        /// <summary>
        /// 客户所在的国家编号
        /// </summary>
        public string CustomerCountryCode { get; set; }

        /// <summary>
        /// 客户的电话号码
        /// </summary>
        public string Telephone { get; set; }

        /// <summary>
        /// 客户的电子邮箱
        /// </summary>
        public string Email { get; set; }

        public string AWD { get; set; }
        public string WizardNum { get; set; }
        public string CouponNum { get; set; }
        public string IATA { get; set; }
        public int IATAType { get; set; }
        public string RateCode { get; set; }

        /// <summary>
        /// 付款类型 1:到付 2:预付
        /// </summary>
        public string PayType { get; set; }

        /// <summary>
        /// 常旅客协议名称
        /// </summary>
        public string FTNProgramme { get; set; }

        /// <summary>
        /// 常旅客号码
        /// </summary>
        public string FTNNumber { get; set; }

        /// <summary>
        /// 航班号
        /// </summary>
        public string FlightNumber { get; set; }

        /// <summary>
        /// 预订车组的唯一码
        /// </summary>
        public string SIPPCode { get; set; }

        /// <summary>
        /// 客户选中的车辆装备等增值服务（结构：类型 + 数量）
        /// </summary>
        public Dictionary<string, int> Equipments { get; set; }

        public string XMembershipID
        {
            get
            {
                var xMembershipID = string.Empty;

                if (!string.IsNullOrEmpty(this.FTNNumber) && !string.IsNullOrEmpty(this.FTNProgramme))
                {
                    xMembershipID = "  <CustLoyalty MembershipID='" + this.FTNNumber + "'  ProgramID='" + this.FTNProgramme + "'/>";
                }
                else if (!string.IsNullOrEmpty(this.FTNNumber))
                {
                    xMembershipID = "  <CustLoyalty MembershipID='" + this.FTNNumber + "'/>";
                }
                else if (!string.IsNullOrEmpty(this.FTNProgramme))
                {
                    xMembershipID = "  <CustLoyalty ProgramID='" + this.FTNProgramme + "'/>";
                }

                if (!string.IsNullOrEmpty(this.WizardNum))
                {
                    xMembershipID += "<CustLoyalty ProgramID='Wizard' MembershipID='" + this.WizardNum + "'/>";
                }

                return xMembershipID;
            }
        }

        /// <summary>
        /// 公司（旅行社、携程）的折扣号的XML
        /// </summary>
        /// <returns></returns>
        public string XCorpDiscountNmbr
        {
            get
            {
                var xmlCorpDiscountNum = string.Empty;
                if (!string.IsNullOrEmpty(RateCode) || !string.IsNullOrEmpty(CouponNum) || !string.IsNullOrEmpty(AWD))
                {
                    var rateCategory = 6;
                    var xmlProp = "";

                    // 套餐号
                    if (!string.IsNullOrEmpty(RateCode))
                    {
                        rateCategory = 2;
                        xmlProp += string.Format(" RateQualifier='{0}' ", RateCode);

                    }

                    // 优惠券号
                    if (!string.IsNullOrEmpty(CouponNum))
                    {
                        xmlProp += string.Format(" PromotionCode='{0}' ", CouponNum);
                    }

                    if (!string.IsNullOrEmpty(AWD))
                    {
                        xmlProp += string.Format(" CorpDiscountNmbr='{0}' ", AWD);
                    }

                    xmlCorpDiscountNum = string.Format("<RateQualifier {0} RateCategory='{1}'/>", xmlProp, rateCategory);
                }

                return xmlCorpDiscountNum;
            }
        }

        public string XAddress
        {
            get
            {
                var xmlProp = "";
                if (!string.IsNullOrEmpty(this.Address))
                {
                    xmlProp += "<AddressLine>" + this.Address + "</AddressLine>";
                }
                if (!string.IsNullOrEmpty(this.CustomerCountryCode))
                {
                    xmlProp += " <CountryName Code='" + this.CustomerCountryCode + "'>" + this.CustomerCountryName + "</CountryName>";
                }
                else
                {
                    // 默认用CN
                    xmlProp += " <CountryName Code='CN'>China</CountryName>";
                }
                if (!string.IsNullOrEmpty(this.Postcode))
                {
                    xmlProp += "<PostalCode>" + this.Postcode + "</PostalCode>";
                }
                if (!string.IsNullOrEmpty(this.CustomerCity))
                {
                    xmlProp += "<CityName>" + this.CustomerCity + "</CityName>";
                }


                if (!(string.IsNullOrWhiteSpace(Address) && string.IsNullOrWhiteSpace(Postcode) && string.IsNullOrWhiteSpace(CustomerCity)))
                {
                    return string.Format("<Address>{0}</Address>", xmlProp);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string XCard
        {
            get
            {
                if (this.DiscountCost > 0 && !string.IsNullOrWhiteSpace(this.AV))
                {
                    var xml = @"<RentalPaymentPref>
                                    <Voucher Identifier='{0}' ValueType='FixedValue' ElectronicIndicator='true'/>
                                    <PaymentAmount Amount='{1}' CurrencyCode='{2}'/>
                                </RentalPaymentPref>";

                    return string.Format(xml, this.AV, this.DiscountCost, this.CurrencyCode);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 协议号的XML
        /// </summary>
        internal string XIATAANN
        {
            get
            {
                string xmlIATAANN = string.Empty;
                if (!string.IsNullOrEmpty(IATA))
                {
                    xmlIATAANN = string.Format("<Source><RequestorID ID='{0}' Type='{1}'/></Source>", IATA, IATAType);
                }
                return xmlIATAANN;
            }
        }

        public string XSpecialEquipmentXML
        {
            get
            {
                if (Equipments == null || Equipments.Count == 0)
                    return string.Empty;

                string xmlSpecialEquipment = "<SpecialEquipPrefs>";
                foreach (var item in Equipments)
                {
                    xmlSpecialEquipment += string.Format("<SpecialEquipPref EquipType='{0}' Quantity='{1}' />", item.Key, item.Value);
                }
                xmlSpecialEquipment += "</SpecialEquipPrefs>";

                return xmlSpecialEquipment;
            }
        }

        public bool CheckRequest()
        {
            if (this.PickupDateTime.Ticks == 0)
            {
                throw new WizardException("无效的取车日期");
            }

            if (this.ReturnDateTime.Ticks == 0)
            {
                throw new WizardException("无效的还车车日期");
            }
            return true;
        }

        public string GetCacheKey()
        {
            return string.Empty;
        }

        public string ToRequestXml()
        {
            var request = this;
            string xmlRequest = string.Empty;
            if (string.IsNullOrWhiteSpace(this.ReservationNumber)) // 新增
            {
                xmlRequest = string.Format(WizardConsts.PayLaterAddOrderXML
                   , request.PickupDateTime
                   , request.ReturnDateTime
                   , request.PickUpStoreCode
                   , request.ReturnStoreCode
                   , request.Firstname
                   , request.Surname
                   , request.XMembershipID
                   , request.SIPPCode
                   , request.XCorpDiscountNmbr
                   , "Status='Available'"
                   , ""
                   , "Res"
                   , request.PayType == "1" ? "<Telephone PhoneNumber='" + request.Telephone + "/EM'></Telephone>" : "<Telephone PhoneNumber='" + request.Telephone + "'></Telephone>"
                   , request.Email == "" ? "" : "<Email>" + request.Email + "</Email>"
                   , request.XAddress
                   , request.XSpecialEquipmentXML
                   , string.IsNullOrEmpty(request.FlightNumber) || request.FlightNumber.Length < 2 ? "" : "Number='" + request.FlightNumber.Substring(2, request.FlightNumber.Length - 2) + "'"
                   , string.IsNullOrEmpty(request.FlightNumber) ? "24" : "14"
                   , string.IsNullOrEmpty(request.FlightNumber) || request.FlightNumber.Length < 2 ? "" : "<MarketingCompany Code='" + request.FlightNumber.Substring(0, 2) + "'/>"
                   , request.XCard
                   , request.XIATAANN
               );
            }
            else  // 修改
            {
                xmlRequest = string.Format(WizardConsts.PayLaterAddOrderXML
                   , request.PickupDateTime
                   , request.ReturnDateTime
                   , request.PickUpStoreCode
                   , request.ReturnStoreCode
                   , request.Firstname
                   , request.Surname
                   , request.XMembershipID
                   , request.SIPPCode
                   , request.XCorpDiscountNmbr
                   , " ModifyType='Modify'"
                   , " <UniqueID Type='14' ID='" + this.ReservationNumber + "'/>"
                   , "Modify"
                   , request.PayType == "1" ? "<Telephone PhoneNumber='" + request.Telephone + "/EM'></Telephone>" : "<Telephone PhoneNumber='" + request.Telephone + "'></Telephone>"
                   , request.Email == "" ? "" : "<Email>" + request.Email + "</Email>"
                   , request.XAddress
                   , request.XSpecialEquipmentXML
                   , string.IsNullOrEmpty(request.FlightNumber) ? "" : "Number='" + request.FlightNumber.Substring(2, request.FlightNumber.Length - 2) + "'"
                   , string.IsNullOrEmpty(request.FlightNumber) ? "24" : "14", string.IsNullOrEmpty(request.FlightNumber) ? "" : "<MarketingCompany Code='" + request.FlightNumber.Substring(0, 2) + "'/>"
                   , request.XCard
                   , request.XIATAANN
                );
            }

            xmlRequest = xmlRequest.Replace("'", "\"")
               .Replace("OutboundUser", WizardConsts.OutboundUser)
               .Replace("OutboundPwd", WizardConsts.OutboundPwd)
               .Replace("OutboundRequestID", WizardConsts.OutboundRequestID);

            return xmlRequest;
        }
    }
}
